﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using Model;

/// <summary>
/// Description résumée de CategorieManager
/// </summary>
/// 
namespace DAL
{
    public class CategorieManager : ICategorieManager
    {
        private IAATOEntities dbService;

        public CategorieManager()
        {
            dbService = new IAATOEntities();
        }

        public bool addCategorie(CATEGORIE categorie)
        {
            try
            {
                dbService.CATEGORIEJeu.AddObject(categorie);
                dbService.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool deleteCategorie(int categorieId)
        {
            try
            {
                CATEGORIE categorie = (from c in dbService.CATEGORIEJeu
                             where c.ID == categorieId
                             select c).First();

                dbService.DeleteObject(categorie);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }

        }

        public List<CATEGORIE> getAllCategories()
        {
            try
            {
                return (from c in dbService.CATEGORIEJeu
                        orderby c.NOM
                        select c).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool updateCategorie()
        {
            try
            {
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public CATEGORIE getCategorieById(int id)
        {
            try
            {
                return (from c in dbService.CATEGORIEJeu
                        where c.ID == id
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string getNom(int id)
        {
            try
            {
                return (from c in dbService.CATEGORIEJeu
                        where c.ID == id
                        select c.NOM).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int getCapaciteById(int id)
        {
            try
            {
                return (from c in dbService.CATEGORIEJeu
                        where c.ID == id
                        select c.CAPACITE).First();
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public CATEGORIE getCategorieByNom(string nom)
        {
            try
            {
                return (from c in dbService.CATEGORIEJeu
                        where c.NOM == nom
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}